package com.lyf.scm.admin.api.controller;

import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.service.SalesPlanOtherDifferinService;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: huangtao
 * @Description:
 * @Date: create in 2020/7/31
 */
@Slf4j
@Api(tags = "其他渠道计划_目标与销售计划差异")
@RequestMapping("/api/v1/salesPlanOtherDifferin")
@RomeController
public class SalesPlanOtherDifferinController {
    @Resource
    private SalesPlanOtherDifferinService salesPlanOtherDifferinService;

    @PostMapping("/listWeek")
    @ApiOperation(value = "按条件查询其他渠道计划_目标与销售计划差异(周)")
    public Response<PlanResponseDTO<SalesPlanOtherDifferinInfoDTO>> listWeekBySelective(@RequestBody SalesPlanOtherDTO salesPlanOtherDTO) {
        return Response.builderSuccess(salesPlanOtherDifferinService.listWeekBySelective(salesPlanOtherDTO));
    }

    @PostMapping("eChartsData")
    @ApiOperation(value = "图表数据接口")
    public Response<EChartsDTO> eChartsData(@RequestBody SalesPlanOtherDTO salesPlanOtherDTO) {
        return Response.builderSuccess(salesPlanOtherDifferinService.eChartsData(salesPlanOtherDTO));
    }

    @PostMapping("/export")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "导出类型-1导出当前页-2导出选中数据-3导出所有数据", required = true, paramType = "query")
    })
    @ApiOperation(value = "导出")
    public void export(@RequestBody ExcelExportDTO excelExportDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse) {
        try {
            //获得feign响应流对象
            feign.Response response = salesPlanOtherDifferinService.export(excelExportDTO, type);
            //如果返回普通json对象，处理异常响应
            if (ExcelExportUtil.validJsonResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                ExcelExportUtil.handleProviderErrorResponse(response, httpServletResponse);
            }
            //如果返回excel对象，处理正确响应
            if (ExcelExportUtil.validExcelResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                String sourceFileName = "其他渠道计划_目标与销售计划差异";
                ExcelExportUtil.handleProviderRightResponse(response, httpServletResponse, sourceFileName);
            }
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }

}
